cmake_minimum_required(VERSION 3.10)

# the name of the target operating system
set(CMAKE_SYSTEM_NAME   Generic)
set(CMAKE_SYSTEM_PROCESSOR  ARM)
set(CROSS_COMPILE_PREFIX arm-none-eabi-)

# specify the cross compiler
set(CMAKE_C_COMPILER ${CROSS_COMPILE_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE_PREFIX}g++)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE_PREFIX}gcc)
set(CMAKE_OBJCOPY ${CROSS_COMPILE_PREFIX}objcopy)
set(CMAKE_OBJDUMP ${CROSS_COMPILE_PREFIX}objdump)
set(CMAKE_SIZE ${CROSS_COMPILE_PREFIX}size)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# Set project name
project(stm32f429i_prj_cmake)

#Set definitions
add_definitions(-DSTM32F429xx)
add_definitions(-DUSE_STM32F429I_DISCO)
add_definitions(-D__TARGET_FPU_VFP)

# Set include files directories
include_directories(
    ${PROJECT_SOURCE_DIR}/../Projects/STM32F429I-Discovery/Example/Inc
    ${PROJECT_SOURCE_DIR}/../Drivers/BSP/STM32F429I-Discovery
    ${PROJECT_SOURCE_DIR}/../Drivers/CMSIS/Device/ST/STM32F4xx/Include
    ${PROJECT_SOURCE_DIR}/../Drivers/CMSIS/Core/Include
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Inc
    ${PROJECT_SOURCE_DIR}/../Middlewares/nr_micro_shell/inc
)

# Set source files
# Driver source files
file(GLOB DRV_SRCS
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
    ${PROJECT_SOURCE_DIR}/../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
    ${PROJECT_SOURCE_DIR}/../Drivers/BSP/STM32F429I-Discovery/stm32f429i_discovery.c
    ${PROJECT_SOURCE_DIR}/../Middlewares/nr_micro_shell/src/*.c
)
# APP source files
file(GLOB APP_SRCS
    ${PROJECT_SOURCE_DIR}/../Projects/STM32F429I-Discovery/Example/Src/*.c
)

# Set startup assembly file 
set(STARTUP_FILE ${PROJECT_SOURCE_DIR}/../Projects/STM32F429I-Discovery/Example/Startup/startup_stm32f429zitx.s)
# Set link file
set(LINKER_SCRIPT ${PROJECT_SOURCE_DIR}/../Projects/STM32F429I-Discovery/Example/Link/STM32F429ZITX_FLASH.ld)

# Enable assembly compiler
enable_language(ASM)

set(DBG_FLAGS "-g -O1")
#Uncomment for hardware floating point
set(FPU_FLAGS "-mfloat-abi=hard -mfpu=fpv4-sp-d16")
add_definitions(-DARM_MATH_CM4 -DARM_MATH_MATRIX_CHECK -DARM_MATH_ROUNDING)
# Set MCU architecture
set(MCU_FLAGS "-mcpu=cortex-m4 ${FPU_FLAGS} -mthumb")
# Set link flag
set(LINK_FLAGS "${MCU_FLAGS} -specs=nano.specs -specs=nosys.specs -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections")
# Set compile and link flags
set(CMAKE_ASM_FLAGS "${MCU_FLAGS} -x assembler-with-cpp ${DBG_FLAGS} " CACHE INTERNAL "ASM compiler flags")
set(CMAKE_C_FLAGS "${MCU_FLAGS} -std=gnu99 -Wall -fdata-sections -ffunction-sections ${DBG_FLAGS} " CACHE INTERNAL "C compiler flags")
set(CMAKE_CXX_FLAGS "${MCU_FLAGS} -fno-rtti -fno-exceptions -fno-builtin -Wall -fdata-sections -ffunction-sections ${DBG_FLAGS} " CACHE INTERNAL "Cxx compiler flags")

set(CMAKE_EXE_LINKER_FLAGS "-Xlinker -T ${LINKER_SCRIPT} ${LINK_FLAGS}" CACHE INTERNAL "Exe linker flags")

# Generate executable target file
add_executable(${PROJECT_NAME}.elf ${STARTUP_FILE} ${APP_SRCS} ${DRV_SRCS})

# User command: generate bin/hex files
add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
COMMAND ${CMAKE_OBJDUMP} -S ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf > ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.disasm
COMMAND ${CMAKE_OBJCOPY} -Obinary ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin
COMMAND ${CMAKE_OBJCOPY} -Oihex ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex
COMMENT "Building binary file and hex file"
)
add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
COMMENT "Invoking: Cross ARM GNU Print Size"
)
